﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Mocola.BLL;
using Mocola.Model;
using Mocola.Web;
using Mocola.Security;
using System.Web.Security;
using System.Data.Common;

namespace Mocola.Net.Web
{
    public partial class newoutfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                string ID = Request.QueryString["ID"].ToString();

                string sql = "SELECT * FROM Moc_OutFiles " +
                             "WHERE ID=" + ID;

                DataSet dtOutFiles = Databases.MocolaNet.ExecuteDataSet(CommandType.Text, sql);

                if (dtOutFiles.Tables[0].Rows.Count == 1)
                {
                    txtWenJianHao.Text = dtOutFiles.Tables[0].Rows[0]["WenJianHao"].ToString();
                    txtMingCheng.Text = dtOutFiles.Tables[0].Rows[0]["Name"].ToString();
                    txtFanWenJiGuan.Text = dtOutFiles.Tables[0].Rows[0]["FaWenJiGuan"].ToString();
                    DateTime dt;
                    if (!string.IsNullOrEmpty(dtOutFiles.Tables[0].Rows[0]["FaWenRiQi"].ToString()))
                    {
                        dt = DateTime.Parse(dtOutFiles.Tables[0].Rows[0]["FaWenRiQi"].ToString());
                        txtFaWenRiQi_Nian.Text = dt.Year.ToString();
                        txtFaWenRiQi_Yue.Text = dt.Month.ToString();
                        txtFaWenRiQi_Ri.Text = dt.Day.ToString();
                    }
                    else
                    {
                        txtFaWenRiQi_Nian.Text = "";
                        txtFaWenRiQi_Yue.Text = "";
                        txtFaWenRiQi_Ri.Text = "";
                    }
                    if (!string.IsNullOrEmpty(dtOutFiles.Tables[0].Rows[0]["JieShouRiQi"].ToString()))
                    {
                        dt = DateTime.Parse(dtOutFiles.Tables[0].Rows[0]["JieShouRiQi"].ToString());
                        txtJieShouRiQi_Nian.Text = dt.Year.ToString();
                        txtJieShouRiQi_Yue.Text = dt.Month.ToString();
                        txtJieShouRiQi_Ri.Text = dt.Day.ToString();
                    }
                    else
                    {
                        txtJieShouRiQi_Nian.Text = "";
                        txtJieShouRiQi_Yue.Text = "";
                        txtJieShouRiQi_Ri.Text = "";
                    }
                    ddlBaoCunQiXian.SelectedValue = dtOutFiles.Tables[0].Rows[0]["BaoCunQiXian"].ToString();
                    txtCunDangHao.Text = dtOutFiles.Tables[0].Rows[0]["CunDangHao"].ToString();
                    ddlStatus.SelectedValue = dtOutFiles.Tables[0].Rows[0]["ZhuangTai"].ToString();
                    txtBeiZhu.Text = dtOutFiles.Tables[0].Rows[0]["BeiZhu"].ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strFaWen = "",strJieShou = "";
            DateTime dtFaWen = DateTime.Parse("1900-1-1"), dtJieShou = DateTime.Parse("1900-1-1");
            if (txtFaWenRiQi_Nian.Text.Trim() != "" && txtFaWenRiQi_Yue.Text.Trim() != "" && txtFaWenRiQi_Ri.Text.Trim() != "")
            {
                try
                {
                    dtFaWen = DateTime.Parse(txtFaWenRiQi_Nian.Text.Trim() + "-" + txtFaWenRiQi_Yue.Text.Trim() + "-" + txtFaWenRiQi_Ri.Text.Trim());
                    strFaWen = dtFaWen.ToShortDateString();
                }
                catch
                {
                    Alert.Show("请输入正确的发文日期!");
                    txtFaWenRiQi_Nian.Focus();
                    return;
                }
            }
            if (txtJieShouRiQi_Nian.Text.Trim() != "" && txtJieShouRiQi_Yue.Text.Trim() != "" && txtJieShouRiQi_Ri.Text.Trim() != "")
            {
                try
                {
                    dtJieShou = DateTime.Parse(txtJieShouRiQi_Nian.Text.Trim() + "-" + txtJieShouRiQi_Yue.Text.Trim() + "-" + txtJieShouRiQi_Ri.Text.Trim());
                    strJieShou = dtJieShou.ToShortDateString();
                }
                catch
                {
                    Alert.Show("请输入正确的接收日期!");
                    txtJieShouRiQi_Nian.Focus();
                    return;
                }
            }

            MembershipCreateStatus status = new MembershipCreateStatus();
            DbTransaction transaction = Databases.MocolaNet.BeginTransaction(IsolationLevel.ReadCommitted);

            if (string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                string pEid = Request.QueryString["eid"].ToString();
                string pTid = Request.QueryString["tid"].ToString();
                try
                {
                    int nID = Databases.MocolaNet.Insert(Moc_OutFilesInfo.Moc_OutFiles)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.Name, txtMingCheng.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.WenJianHao, txtWenJianHao.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.FaWenRiQi, strFaWen)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.FaWenJiGuan, txtFanWenJiGuan.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.JieShouRiQi, strJieShou)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.BaoCunQiXian, ddlBaoCunQiXian.SelectedValue)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.CunDangHao, txtCunDangHao.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.ZhuangTai, ddlStatus.SelectedValue)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.BeiZhu, txtBeiZhu.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.Eid, pEid)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.Tid, pTid)
                                .SetTransaction(transaction)
                                .ExecuteReturnAutoIncrementID(Moc_OutFilesInfo.Moc_OutFiles.ID);

                    bool blTrees = false;

                    if (nID != -1)
                    {
                        blTrees = true;
                    }

                    if (blTrees)
                    {
                        Alert.Show("保存成功!");
                        status = MembershipCreateStatus.Success;
                    }
                    else
                    {
                        Alert.Show("保存失败!");
                        status = MembershipCreateStatus.ProviderError;
                    }
                }
                catch (Exception ex)
                {
                    Alert.Show(ex.Message);
                    status = MembershipCreateStatus.ProviderError;
                }
                finally
                {
                    if (status == MembershipCreateStatus.Success)
                        transaction.Commit();
                    else
                        transaction.Rollback();
                    Databases.MocolaNet.CloseConnection(transaction);
                }
            }
            else
            {
                try
                {
                    int ID = Convert.ToInt32(Request.QueryString["ID"]);
                    string pEid = Request.QueryString["eid"].ToString();
                    string pTid = Request.QueryString["tid"].ToString();
                    bool bUpdated = Databases.MocolaNet.Update(Moc_OutFilesInfo.Moc_OutFiles)
                                    .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.Name, txtMingCheng.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.WenJianHao, txtWenJianHao.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.FaWenRiQi, strFaWen)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.FaWenJiGuan, txtFanWenJiGuan.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.JieShouRiQi, strJieShou)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.BaoCunQiXian, ddlBaoCunQiXian.SelectedValue)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.CunDangHao, txtCunDangHao.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.ZhuangTai, ddlStatus.SelectedValue)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.BeiZhu, txtBeiZhu.Text.Trim())
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.Eid, pEid)
                                .AddColumn(Moc_OutFilesInfo.Moc_OutFiles.Tid, pTid)
                                    .Where(Moc_OutFilesInfo.Moc_OutFiles.ID == ID)
                                    .SetTransaction(transaction)
                                    .Execute() == 1;

                    if (bUpdated)
                    {
                        Alert.Show("更新成功!");
                        status = MembershipCreateStatus.Success;
                    }
                    else
                    {
                        Alert.Show("更新失败!");
                        status = MembershipCreateStatus.ProviderError;
                    }
                }
                catch (Exception ex)
                {
                    Alert.Show(ex.Message);
                    status = MembershipCreateStatus.ProviderError;
                }
                finally
                {
                    if (status == MembershipCreateStatus.Success)
                        transaction.Commit();
                    else
                        transaction.Rollback();
                    Databases.MocolaNet.CloseConnection(transaction);
                }
            }
        }
    }
}
